package com.wzh.activity.mq;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.wzh.activity.dao.ActivityDao;
import com.wzh.activity.dao.OrderInfoDao;
import com.wzh.activity.entity.ActivityEntity;
import com.wzh.activity.entity.GoodsEntity;
import com.wzh.activity.entity.OrderInfoEntity;
import com.wzh.activity.feign.GoodsFeign;
import com.wzh.activity.vo.OrderVo;
import com.wzh.common.utils.R;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author WZH
 * @title: Consumer
 * @projectName miaosha
 * @description:
 * @date 2022/2/28
 */
@Component
public class Consumer {
    @Autowired
    private OrderInfoDao orderInfoDao;

    @Autowired
    private ActivityDao activityDao;

    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(queues ="msg")
    public void take01(OrderVo orderVo, Channel channel, Message message) throws IOException {
        //查询商品 确保订单的安全性关于金额都去查数据库
        R info = goodsFeign.info(orderVo.getGoodsId());
        if (info.getCode()==0){
            Object goods = info.get("goods");
            String jsonString = JSON.toJSONString(goods);
            GoodsEntity goodsEntity = JSON.parseObject(jsonString, GoodsEntity.class);
            //查询活动
            ActivityEntity activityEntity = activityDao.selectById(orderVo.getActivityId());
            OrderInfoEntity orderInfo = new OrderInfoEntity();
            String orderNo = UUID.randomUUID().toString().replace("-", "");
            orderInfo.setUserId(Long.valueOf(orderVo.getUserId()));
            orderInfo.setGoodsId(orderVo.getGoodsId());
            orderInfo.setGoodsName(goodsEntity.getGoodsName());
            orderInfo.setGoodsCount(1);
            orderInfo.setGoodsPrice(activityEntity.getMiaoshaPrice());
            orderInfo.setGoodsChannel(orderVo.getGoodsChannel());
            orderInfo.setStatus(0);
            orderInfo.setCreateDate(new Date());
            orderInfo.setActivityId(orderVo.getActivityId());
            orderInfo.setOrderNo(orderNo);
            //订单生成
            orderInfoDao.insert(orderInfo);
            //redis活动库存-1
            ActivityEntity activity = (ActivityEntity) redisTemplate.boundHashOps("activity").get("item" + orderVo.getActivityId());

            //如果库存为0，修改数据库
            if(activity.getStockCount()<=0){
                //activity.setStockCount(0);
                activityDao.updateById(activity);
            }
        }
        //商品库存-1
//        R info = goodsFeign.info(order.getGoodsId());
//        if(info.getCode()==0){
//            Object goods = info.get("goods");
//            String jsonString = JSON.toJSONString(goods);
//            GoodsEntity goodsEntity = JSON.parseObject(jsonString, GoodsEntity.class);
//            goodsEntity.setGoodsStock(goodsEntity.getGoodsStock()-order.getGoodsCount());
//            goodsFeign.update(goodsEntity);
//        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues ="msg2")
    public void take02(ActivityEntity activity, Channel channel, Message message) throws IOException {
        activityDao.updateById(activity);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
